﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Telerik.Windows.Controls;
using System.Collections.Generic;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCL.UI.PopWindows;

namespace Baosight.ZNH.ZMCL.UI
{
    public partial class ZMCL10 : EFViewCmes
    {
        private EIBlock sourceBlock;
        private EIBlock pathBlock;
        private EIBlock unitBlock;
        private EIBlock peopleBlock;

        public ZMCL10()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            //设置下拉框block
            sourceBlock = initData.GetBlock("cmes.cl.noteSource");
            pathBlock = initData.GetBlock("cmes.cl.securityPath");
            unitBlock = initData.GetBlock("cmes.cl.department");
            peopleBlock = initData.GetBlock("cmes.cl.responsibilityPeople");

            //初始化查询区
            this.fdNotesource.Value = "";

            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击反馈按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup("反馈", eiInfo, "工单反馈-反馈", "update", null, sender, e);
        }

        /// <summary>
        /// 反馈前的判断
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            bool modifyFlag = true;
            if ("完成".Equals(_eIRows[0]["fdStatus"].ToString()))
            {
                base.OpenEFWindowAlert("该巡检工单状态为完成，不能进行反馈！", true);
                modifyFlag = false;
            }
            return modifyFlag;
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
                this.UpdateHomePageData(new EIInfo());//发布数据改变
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 明细
        /// By 严洪浩 2011.4.20 10:41
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;

            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "工单反馈-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            InspectNoteFeedback noteFeedback = new InspectNoteFeedback(_dialogType);//构造一个新的修改页面
            noteFeedback.EIDataContext = new Platform.Core.UI.EIDataContext(); 
            _eIInfo.AddBlock(sourceBlock);
            _eIInfo.AddBlock(pathBlock);
            _eIInfo.AddBlock(unitBlock);
            _eIInfo.AddBlock(peopleBlock);
            noteFeedback.EIDataContext.Update(_eIInfo);
            if(!_dialogType.Equals("detail"))
            _eIInfo.GetBlock("result").Set(0, "fdFeedback", null);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = noteFeedback;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

    }
}
